import { defineStore } from 'pinia'
import request from '@/utils/request'
import { ApiRes, CategoryItem } from '@/types/data'
import { topCategory } from '../constains'
// 定义初始值
const defaultList = topCategory.map(item => {
    return {
        name: item
    }
})

export default defineStore('category', {
    state: () => ({
        list: defaultList as CategoryItem[]
    }),
    actions: {
        async getAllCategory() {
            const res = await request.get<ApiRes<CategoryItem[]>>('/home/category/head')
            // 给每个一个分类添加open属性，用于显示隐藏
            res.data.result.forEach(item => {
                item.open = false
            })
            this.list = res.data.result
        },
        show(id: string) {
            const category = this.list.find(item => item.id === id)!
            category.open = true
        },
        hide(id: string) {
            const category = this.list.find(item => item.id === id)!
            category.open = false
        }
    }
})